package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Client;
import cn.wolfcode.edu.domain.ClientTrace;
import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.mapper.ClientMapper;
import cn.wolfcode.edu.mapper.ClientTraceMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IClientTraceService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientTraceServiceImpl implements IClientTraceService {

    @Autowired
    private ClientTraceMapper clientTraceMapper;
    @Autowired
    private ClientMapper clientMapper;

    public void save(ClientTrace clientTrace) {
        Client client = clientTrace.getClient();
        client.setLastTraceTime(new Date());
        //跟进次数+1 待完成
        Client oldClient = clientMapper.selectByPrimaryKey(clientTrace.getClient().getId());
        client.setTraceNum(oldClient.getTraceNum() + 1);
        //将上次跟进时间改为当前时间
        clientTraceMapper.insert(clientTrace);
        //更新客户信息
        clientMapper.updateByTrace(client);

    }


    public PageResult query(QueryObject qo) {
        //查询数据数
        Integer total = clientTraceMapper.queryForCount(qo);
        //查询数据集
        List<ClientTrace> rows =  clientTraceMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void audit(ClientTrace clientTrace) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //将审核人设置为当前登录用户
        clientTrace.setAuditor(employee);
        //将审核状态设置为已审核
        clientTrace.setStatue(ClientTrace.YES_AUDIT);
        clientTraceMapper.audit(clientTrace);
    }
}
